package xyz.riceball.security.authentication;

import lombok.Getter;
import lombok.Setter;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.ProviderNotFoundException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import java.util.Objects;

/**
 * <p>
 * 认证的Manager
 * </p>
 *
 * @author xiaovcloud
 * @since 2022/4/27 11:59
 */
@Getter
@Setter
public class RiceBallAuthenticationManager extends ProviderManager {

    private final RiceBallAuthenticationProvider authenticationProvider;

    public RiceBallAuthenticationManager(RiceBallAuthenticationProvider authenticationProvider) {
        super(authenticationProvider);
        this.authenticationProvider = authenticationProvider;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        Authentication result = authenticationProvider.authenticate(authentication);
        if (Objects.nonNull(result)) {
            return result;
        }
        throw new ProviderNotFoundException("Authentication failed!");
    }
}
